home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / src890906.arc / FTPSUBR.C < prev    next >
C/C++ Source or Header  |  1989-04-16  |  2KB  |  127 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "ftp.h"
  6.  
  7. /* Send a file (opened by caller) on a network socket */
  8. long
  9. sendfile(fp,s,mode)
  10. FILE *fp;    /* File to be sent */
  11. int s;        /* Socket to be sent on */
  12. int mode;    /* Transfer mode */
  13. {
  14.     register struct mbuf *bp;
  15.     register char *cp;
  16.     int c;
  17.     long total = 0;
  18.  
  19.     switch(mode){
  20.     default:
  21.     case LOGICAL_TYPE:
  22.     case IMAGE_TYPE:
  23.         for(;;){
  24.             if((bp = alloc_mbuf(BLKSIZE)) == NULLBUF)
  25.                 return -1;
  26.             if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
  27.                 free_p(bp);
  28.                 break;
  29.             }
  30.             total += bp->cnt;
  31.             if(send_mbuf(s,bp,0,NULLCHAR,0) == -1)
  32.                 return -1;
  33.         }
  34.         break;
  35.     case ASCII_TYPE:
  36.         for(;;){
  37.             if((bp = alloc_mbuf(BLKSIZE+1)) == NULLBUF)
  38.                 return -1;
  39.             cp = bp->data;
  40.             while(bp->cnt < BLKSIZE && (c = fgetc(fp)) != EOF){
  41.                 switch(c){
  42.                 case '\r':
  43.                     continue;
  44.                 case '\n':
  45.                     *cp++ = '\r';
  46.                     *cp++ = '\n';
  47.                     bp->cnt += 2;
  48.                     break;
  49.                 default:
  50.                     *cp++ = c;
  51.                     bp->cnt++;
  52.                     break;
  53.                 }
  54.             }                
  55.             if(bp->cnt != 0){
  56.                 total += bp->cnt;
  57.                 if(send_mbuf(s,bp,0,NULLCHAR,0) == -1)
  58.                     return -1;
  59.             } else {
  60.                 free_p(bp);
  61.                 break;
  62.             }
  63.         }
  64.         break;
  65.     }
  66.     return total;
  67. }
  68. long
  69. recvfile(fp,s,mode)
  70. FILE *fp;
  71. int s;
  72. int mode;
  73. {
  74.     int cnt;
  75.     char c;
  76.     struct mbuf *bp;
  77.     long total = 0;
  78.  
  79.     switch(mode){
  80.     default:
  81.     case LOGICAL_TYPE:
  82.     case IMAGE_TYPE:
  83.         while((cnt = recv_mbuf(s,&bp,0,0,NULLCHAR,0)) != 0){
  84.             if(cnt == -1)
  85.                 return -1;
  86.  
  87.             total += cnt;
  88.             if(write_p(fp,bp) == -1){
  89.                 free_p(bp);
  90.                 return -1;
  91.             }
  92.             free_p(bp);
  93.         }
  94.         break;
  95.     case ASCII_TYPE:
  96.         while((cnt = recv_mbuf(s,&bp,0,0,NULLCHAR,0)) != 0){
  97.             if(cnt == -1)
  98.                 return -1;
  99.  
  100.             total += cnt;
  101.             while(pullup(&bp,&c,1) == 1){
  102.                 switch(uchar(c)){
  103.                 case '\r':
  104.                     continue;
  105.                 case '\n':
  106. #if !defined(UNIX) && !defined(__TURBOC__)
  107.                     /* Needed only if the OS uses a CR/LF
  108.                      * convention and fputc doesn't do
  109.                      * an automatic translation
  110.                      */
  111.                     fputc('\r',fp);
  112. #endif
  113.                     fputc('\n',fp);
  114.                     break;
  115.                 default:
  116.                     if(fputc(c,fp) == EOF){
  117.                         free_p(bp);
  118.                         return -1;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         break;
  124.     }
  125.     return total;
  126. }
  127.